package com.resou.reader.choosegender.giftbook;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.resou.reader.MainActivity;
import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.LargessBook;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.reader.utils.ScreenUtils;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.TextUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GiftBooksActivity extends ResouBaseActivity<GiftBooksPresenter> implements View.OnClickListener, GiftBooksContract.View {

    public static final String CATEGORIES = "categories";
    public static final String GENDER = "gender";
    int screenWidth = ReSouApplication.SCREEN_WIDTH;
    int screenHeight = ReSouApplication.SCREEN_HEIGHT;

    public static int START_FROM_POSITION = 0;
    public static final int START_FROM_DETERMINE = 1;   //选择类别
    public static final int START_FROM_SKIP = 2;        //点击跳过

    @BindView(R.id.tv_skip)
    TextView tvSkip;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.v_divider)
    View vDivider;
    @BindView(R.id.tv_change)
    TextView tvChange;
    @BindView(R.id.tv_add_to_bookshelf)
    TextView tvAddToBookshelf;
    @BindView(R.id.tv_new_user)
    TextView tvNewUser;
    @BindView(R.id.tv_gift_book)
    TextView tvGiftBook;
    @BindView(R.id.tv_recommend)
    TextView tvRecommend;
    private int mImageWidth;
    private String mGender;
    private AlreadyGivenAdapter mAdapter;
    private String mCategories;

    //选择类别后进入
    public static void start(Context context, String gender, String categories) {
        Intent starter = new Intent(context, GiftBooksActivity.class);
        starter.putExtra(GENDER, gender);
        starter.putExtra(CATEGORIES, categories);
        START_FROM_POSITION = START_FROM_DETERMINE;
        context.startActivity(starter);
    }

    //跳过后进入
    public static void start(Context context) {
        Intent starter = new Intent(context, GiftBooksActivity.class);
        START_FROM_POSITION = START_FROM_SKIP;
        context.startActivity(starter);
    }

    @Override
    protected void initStatusBar() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter = new GiftBooksPresenter(this);
    }

    private static final String TAG = "AlreadyGivenBooks";

    @Override
    protected void initView() {
        int dp22 = (int) ScreenUtils.convertDpToPixel(22, this);
//        int dp4 = (int) ScreenUtils.convertDpToPixel(4, this);
//        int dp25 = (int) ScreenUtils.convertDpToPixel(25, this);
        mImageWidth = (screenWidth - dp22 * 4) / 3;
//        String strings = getIntent().getStringExtra("strings");
        mAdapter = new AlreadyGivenAdapter();
        recyclerView.setAdapter(mAdapter);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, dp22, true));
        tvSkip.setOnClickListener(this);
        tvAddToBookshelf.setOnClickListener(this);
        tvChange.setOnClickListener(this);
        if (START_FROM_POSITION == START_FROM_DETERMINE) {
            mGender = getIntent().getStringExtra(GENDER);
            mCategories = getIntent().getStringExtra(CATEGORIES);
        }
        if (START_FROM_POSITION == START_FROM_SKIP) {
            tvChange.setVisibility(View.GONE);
            vDivider.setVisibility(View.GONE);
            tvRecommend.setVisibility(View.GONE);
            tvGiftBook.setVisibility(View.VISIBLE);
            tvNewUser.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_already_given_books;
    }

    @Override
    public void retryRefreshData() {

    }

    @Override
    public void onClick(View v) {
        if (v == tvSkip) {
//            PreferenceUtil.getInstance(this).saveUserGender(Integer.valueOf("3"));
            mPresenter.skipChoose("3", "");
            MainActivity.start(this);
            finish();
        }
        if (v == tvChange) {
            if (isNetwork()) {
                initData();
            }
        }
        if (v == tvAddToBookshelf) {
            if (isNetwork()) {
                if (mGender == null) {
                    mGender = "3";
                }
//            PreferenceUtil.getInstance(this).saveUserGender(Integer.valueOf(mGender));
                mPresenter.save(mAdapter.getLargessBooks());
                mPresenter.synchronizeBooks(TextUtils.getFreeBooks2(mAdapter.getLargessBooks()));
            }


        }
    }

    @Override
    public void initData() {
        mPresenter.loadBooks(mGender, mCategories);
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void setBooks(List<LargessBook> data) {
        mAdapter.setData(data);
    }

    @Override
    public void finishPage() {
        MainActivity.start(this);
        finish();
    }

    class AlreadyGivenAdapter extends RecyclerView.Adapter<AlreadyGivenHolder> {

        private List<LargessBook> mLargessBooks;

        public AlreadyGivenAdapter() {
            mLargessBooks = new ArrayList<>();
        }

        public void setData(List<LargessBook> data) {
            mLargessBooks = data;
            notifyDataSetChanged();
        }

        public List<LargessBook> getLargessBooks() {
            return mLargessBooks;
        }

        @NonNull
        @Override
        public AlreadyGivenHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            AlreadyGivenHolder holder = new AlreadyGivenHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_item_already_given_book, viewGroup, false));
            ViewGroup.LayoutParams layoutParams = holder.ivBookImage.getLayoutParams();
//            layoutParams.width = mImageWidth;
            layoutParams.height = (int) (mImageWidth * 1.322);
//            holder.tvBookName.setMaxWidth(mImageWidth);
            return holder;
        }

        private static final String TAG = "AlreadyGivenAdapter";

        @Override
        public void onBindViewHolder(@NonNull AlreadyGivenHolder holder, int position) {
            LargessBook book = mLargessBooks.get(position);
            holder.tvBookName.setText(book.getBookName());
            Glide.with(GiftBooksActivity.this).load(book.getBookPic()).into(holder.ivBookImage);
        }

        @Override
        public int getItemCount() {
            return mLargessBooks == null ? 0 : mLargessBooks.size();
        }
    }

    static class AlreadyGivenHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_book_image)
        ImageView ivBookImage;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;

        public AlreadyGivenHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
